<#include "/common/defaultEngine.html"/>
<@pageTheme mark="${config.optimize()?string('true', 'false')}">
    <@header title="用户列表" bodyClass="gray-bg animated fadeInRight" isAnimation=false libs=["table","zTree","layout-latest"]/>
    <div class="ui-layout-west">
        <div class="box box-main">
            <div class="box-header">
                <div class="box-title">
                    <i class="fa icon-grid"></i> 组织机构
                </div>
                <div class="box-tools pull-right">
                    <a type="button" class="btn btn-box-tool" href="#" onclick="comp()" title="公司管理"><i class="fa fa-edit"></i></a>
                    <button type="button" class="btn btn-box-tool" id="btnExpand" title="展开" style="display:none;"><i class="fa fa-chevron-up"></i></button>
                    <button type="button" class="btn btn-box-tool" id="btnCollapse" title="折叠"><i class="fa fa-chevron-down"></i></button>
                    <button type="button" class="btn btn-box-tool" id="btnRefresh" title="刷新部门"><i class="fa fa-refresh"></i></button>
                </div>
            </div>
            <div class="ui-layout-content">
                <div id="tree" class="ztree"></div>
            </div>
        </div>
    </div>
    <div class="ui-layout-center">
        <div class="container-div">
            <div class="row">
                <div class="col-sm-12 search-collapse">
                    <@f.form id="user-form">
                        <div class="select-list">
                            <ul>
                                <li>
                                    <label><@ctx.i18n text = "账号"/></label>：
                                    <@f.input name="username"/>
                                </li>
                                <li>
                                    <@ctx.i18n text = "手机"/>：
                                    <@f.input name="mobile"/>
                                </li>
                                <li>
                                    <label><@ctx.i18n text = "归属机构"/></label>：
                                    <div class="control-div width-120">
                                        <@f.treeselect name="compId" boxTitle="请选择归属机构" url="sys/comp/treeData"
                                        placeholder="请选择上级机构"/>
                                    </div>
                                </li>
                                <li>
                                    <label><@ctx.i18n text = "状态"/></label>：
                                    <@f.select name="status" dictType="sys_normal_disable" blankValue="" blankLabel="所有"/>
                                </li>
                                <li>
                                    <a class="btn btn-primary btn-rounded btn-sm" onclick="$.table.search()"><i class="fa fa-search"></i>&nbsp;<@ctx.i18n text = "查询"/></a>
                                    <a class="btn btn-warning btn-rounded btn-sm" onclick="opt.form.reset()"><i class="fa fa-refresh"></i>&nbsp;<@ctx.i18n text = "重置"/></a>
                                    <a class="btn btn-default btn-rounded btn-sm" data-toggle="formMore"><i class="fa fa-angle-double-down"></i>&nbsp;<@ctx.i18n text = "更多"/></a>
                                </li>
                            </ul>
                        </div>
                        <div class="select-list-more">
                            <ul>
                                <li>
                                    <label><@ctx.i18n text = "姓名"/></label>：
                                    <@f.input name="name"/>
                                </li>
                                <li>
                                    <label><@ctx.i18n text = "邮箱"/></label>：
                                    <@f.input name="email"/>
                                </li>
                            </ul>
                        </div>
                    </@f.form>
                </div>
                <@table id="bootstrap-table">
                    <@shiro.hasPermission name="sys:user:add">
                        <a class="btn btn-success" onclick="opt.operate.addTab()"><i class="fa fa-plus"></i>&nbsp;<@ctx.i18n text = "新增"/></a>
                    </@shiro.hasPermission>
                    <@shiro.hasPermission name="sys:user:edit">
                        <a class="btn btn-primary single disabled" onclick="opt.operate.edit()"><i class="fa fa-pencil-square-o"></i>&nbsp;<@ctx.i18n text = "修改"/></a>
                    </@shiro.hasPermission>
                    <@shiro.hasPermission name="sys:user:del">
                        <a class="btn btn-danger multiple disabled" onclick="opt.operate.delAll()">
                            <i class="fa fa-remove"></i> <@ctx.i18n text = "删除"/>
                        </a>
                    </@shiro.hasPermission>
<#--                    <@shiro.hasPermission name="sys:user:export">-->
<#--                        <a class="btn btn-warning" onclick="$.table.exportExcel()">-->
<#--                            <i class="fa fa-download"></i> 导出-->
<#--                        </a>-->
<#--                    </@shiro.hasPermission>-->
                    <@shiro.hasPermission name="sys:user:export">
                        <a class="btn btn-warning" onclick="opt.modal.windowOpen(baseURL+'ureport/preview?_u=fast-userInfo.ureport.xml&_t=1,2,3,4,5,6,7&tenantId=<@shiro.principal property="tenantId"/>')">
                            <i class="fa fa-download"></i> 导出\预览
                        </a>
                    </@shiro.hasPermission>
<#--                    <@shiro.hasPermission name="sys:user:export">-->
<#--                        <a class="btn btn-warning" onclick="reloadMaper()">-->
<#--                            <i class="fa fa-download"></i> 同步BPM-->
<#--                        </a>-->
<#--                    </@shiro.hasPermission>-->
                </@table>
            </div>
        </div>
    </div>
    <@footer>
        <script type="text/javascript">

            var editFlag = "${permission.hasPermi('sys:user:edit')}";
            var delFlag = "${permission.hasPermi('sys:user:del')}";
            var resetPwdFlag = "${permission.hasPermi('sys:user:resetPwd')}";
            var visible = ${permission.isPermitteds("sys:user:edit,sys:user:del,sys:user:resetPwd")?string('true', 'false')};
            var datas = ${dict.getDictListJson('sys_user_show_hide')};
            var prefix = baseURL + 'sys/user';

            function reloadMaper() {
                opt.modal.confirm("确定要更新BPM吗?", function() {
                    var url = prefix + "/bpmSynchronizationUser";
                    opt.operate.post(url);
                })
            }

            $(function() {
                /*初始化页面布局*/
                var panehHidden = false;
                if ($(this).width() < 767) {
                    panehHidden = true;
                }
                /*初始化布局*/
                $('body').layout({
                    initClosed: panehHidden
                    , west__size: 190
                    ,west__fxSpeed_size:			"fast"	// 'fast' animation when resizing west-pane
                    ,west__fxSpeed_open:			1000	// 1-second animation when opening west-pane
                    ,west__fxSettings_open:		{ easing: "easeOutBounce" } // 'bounce' effect when opening
                    ,west__fxName_close:			"none"	// NO animation when closing west-pane
                });
                /*加载 树、树表格*/
                initTable();
                initLeftZtree();
            });

            function initTable() {
                /* 初始表格对象 */
                var options = {
                    url: prefix + "/list",
                    addUrl: prefix + "/add",
                    editUrl: prefix + "/edit/{id}",
                    delUrl: prefix + "/del",
                    viewUrl: prefix + "/view/{id}",
                    exportUrl:  prefix + "/export",
                    clickToSelect: false,
                    // showBorder: true, // 是否显示表格边框
                    pageSize: 12,
                    pageList: [12, 24, 36],
                    sortName: "createTime",
                    // fixedColumns: true,
                    // fixedNumber: 3,
                    // rightFixedColumns: true,
                    // rightFixedNumber: 2,
                    uniqueId: "id", /* 唯一ID */
                    modalName: "用户",
                    columns:[
                        {
                            title: "<@ctx.i18n text = "序号"/>",width:35, formatter: function (value, row, index) {
                                return $.table.serialNumber(index);
                            }
                        },
                        <#--{field: 'id', title: "<@ctx.i18n text = "用户ID"/>", sortable: true, visible: false},-->
                        {field: 'username', title: "<@ctx.i18n text = "账号"/>",formatter: function(value, row, index){
                                return '<a class="tb-view" onclick="opt.operate.view(\''+row.id+'\',editFlag,opt.operate.edit)">'+value+'</a>';
                            }
                        },
                        {field: 'name', title:  "<@ctx.i18n text = "姓名"/>"},
                        {field: 'compName', valign: 'middle', title: "<@ctx.i18n text = "归属公司"/>",formatter: function (value, row, index) {
                                return $.table.tooltip(value,5);}
                        },
                        {field: 'deptName', title:  "<@ctx.i18n text = "归属机构"/>"},
                        {field: 'roleName', title: "<@ctx.i18n text = "角色"/>",formatter: function (value, row, index) {
                                return $.table.tooltip(value,5);}
                        },
                        {field: 'email', title:  "<@ctx.i18n text = "邮箱"/>"},
                        {field: 'mobile', title:  "<@ctx.i18n text = "手机"/>"},
                        {
                            field: 'status', title: "<@ctx.i18n text = "状态"/>", formatter: function (value, row, index) {
                                return $.table.selectDictLabel(datas, value);
                            }
                        },
                        {field: 'createTime', title: "<@ctx.i18n text = "创建时间"/>"},
                        {title: "<@ctx.i18n text = "操作"/>", align: 'left',visible:visible,class:'action', formatter: function (value, row, index) {
                                var actions = [];
                                actions.push('<a class="' + editFlag + '" href="javascript:void(0)" title="<@ctx.i18n text = "修改"/>" onclick="opt.operate.edit(\'' + row.id + '\')"><i class="fa fa-edit"></i></a> ');
                                if(row.status === "0"){
                                    actions.push('<a class="' + delFlag + '" href="javascript:void(0)" title="<@ctx.i18n text = "停用"/>" onclick="disable(\'' + row.id + '\')"><i class="glyphicon glyphicon-ban-circle"></i></a> ');
                                }else{
                                    actions.push('<a class="' + delFlag + '" href="javascript:void(0)" title="<@ctx.i18n text = "启用"/>" onclick="enable(\'' + row.id + '\')"><i class="glyphicon glyphicon-ok-circle"></i></a> ');
                                }
                                actions.push('<a class="' + delFlag + '" href="javascript:void(0)" title="<@ctx.i18n text = "删除"/>" onclick="opt.operate.del(\'' + row.id + '\')"><i class="fa fa-trash-o"></i></a> ');
                                var info = "<a  class='btn btn-default btn-xs' onclick=javascript:resetPwd(\'" + row.id + "\');><i class='fa icon-key'></i> <@ctx.i18n text = "重置密码"/></a>&nbsp;";
                                info += "<a  class='btn btn-default btn-xs' onclick=javascript:authRole(\'" + row.id +"\');><i class='fa icon-check'></i> <@ctx.i18n text = "分配角色"/></a>";
                                actions.push('<a tabindex="0" class="' + resetPwdFlag + '" role="button" data-container="body" ' +
                                    'data-placement="left" data-toggle="popover" data-html="true" data-trigger="hover" ' +
                                    'data-content="'+info+'"><i class="fa fa-chevron-circle-right" title="<@ctx.i18n text = "更多操作"/>"></i></a>');
                                return actions.join('');
                            }
                        }
                    ]
                };
                $.table.init(options);
            }


            /**
             * opt.operate.edit, opt.operate.delAll 提交数据前校验
             * id 表格对象ID
             * event 表格事件名称
             * rows 获取的数据
             * return true 数据才会提交 反之 false
             **/
            function bootTableCallback(id,event,rows){
                console.log('表格编辑删除事件回调 bootTableCallback ---->>> id:[' + id + "] event:[" + event + "] rows:[" + JSON.stringify(rows) + "]");
                /* 简单逻辑示例
                if(id == 'bootstrap-table'){
                    // 删除、编辑拦截
                    if(event == 'del' || event == 'edit'){
                        var flag = true;
                        for(var i=0; i< rows.length; i++){
                            if(rows[i].username == 'admin'){
                                flag = false;
                                opt.error('最大管理员用户不能删除!');
                            }
                        }
                        return flag;
                    }
                }*/
                return true;
            }

            /*初始化左侧树*/
            function initLeftZtree() {
                var options = {
                    url: baseURL+"sys/comp/treeData",
                    displayLen: 12,
                    expandLevel:1,
                    onClick : onClick,
                    check: {
                        enable: false
                    }
                };
                $.tree.init(options);
            }

            /* 点击左侧数节点 回调事件 刷新表格*/
            function onClick(event, treeId, treeNode) {
                /* 设置属性值*/
                $("#compId").val(treeNode.id);
                $("#compIdName").val(treeNode.name);
                $.table.search();
            }

            /* 左侧数刷新事件*/
            $('#btnRefresh').click(function() {
                initLeftZtree();
            });

            /* 左侧树展开事件 */
            $('#btnExpand').click(function() {
                $._tree.expandAll(true);
                $(this).hide();
                $('#btnCollapse').show();
            });

            /* 左侧树折叠事件 */
            $('#btnCollapse').click(function() {
                $._tree.expandAll(false);
                $(this).hide();
                $('#btnExpand').show();
            });

            function comp() {
                var url = baseURL + "sys/dept";
                opt.modal.openTab("机构管理", url);
            }

            /* 点击树组件 回调事件案例 回调方法名固定 刷新表格*/
            /**
             *  id 为组件id
             *  event 为组件点击按钮名称 ok 确定 clear 清除 cancel 取消
             *  index 弹出窗口index
             *  layero
             *  nodes 确认事件传的参数
             **/
            function treeCallback(id, event, index, layero, nodes) {
                if(id == "compId"){
                    if(event == "ok"){
                        /* 点击确认按钮直接刷新表格*/
                        $.table.search();
                    }
                }
            }

            /* 用户管理-重置密码 */
            function resetPwd(userId) {
                var url = prefix + '/resetPwd/' + userId;
                opt.modal.open("重置密码", url,'800', '225');
            }

            /* 用户管理-停用 */
            function disable(userId) {
                // opt.modal.confirm("确认要停用用户吗？", function() {
                //     opt.operate.post(prefix + "/changeStatus", { "id": userId, "status": 1 });
                // })
                var rows =  $.table.selectAllColumnRows();
                console.log(rows);
                console.log($.table.selectAllColumns());
            }

            /* 用户管理启用 */
            function enable(userId) {
                // opt.modal.confirm("确认要启用用户吗？", function() {
                //     opt.operate.post(prefix + "/changeStatus", { "id": userId, "status": 0 });
                // })
                var rows =  $.table.selectAllColumnRows();
                console.log(rows);
                console.log($.table.selectAllColumns());
            }

            /* 授权角色*/
            function authRole(userId) {
                opt.modal.openTab("<@ctx.i18n text = "用户分配角色"/>", prefix + "/authRole/" + userId);
                return;
            }

        </script>
    </@footer>
</@pageTheme>